<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class VillageThings extends Model
{
    use SoftDeletes;
    protected $table = 'village_things';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];

    /*追加字段*/
    protected $appends = ['zaned','followed','imgs_url','video_img_url','video_url','content_intro'];


    public function villager()
    {
        return $this -> hasOne(User::class,'id','user_id');
    }

    public function comments(){
        return $this -> hasOne(VillageThingsComment::class, 'village_things_id','id');
    }

    // 所属地区
    public function region_info()
    {
        return $this->hasOne('\App\Models\SystemRegion', 'id', 'region_id');
    }


    public function getImgsUrlAttribute()
    {
        $newarr = [];
        if($this -> attributes['imgs']){
            $arr = json_decode($this -> attributes['imgs']);
            foreach($arr as $v){
                $newarr[] = config('app.img_url') . $v;
            }
        }
        return $newarr;
    }


    public function getVideoImgUrlAttribute()
    {
        if($this -> attributes['video_img']){
            return config('app.img_url') . $this -> attributes['video_img'];
        }
        return '';
    }

    public function getVideoUrlAttribute()
    {
        if($this -> attributes['video']){
            return config('app.img_url') . $this -> attributes['video'];
        }
        return '';
    }

    public function getContentIntroAttribute()
    {
        $content_intro = strip_tags($this -> content);
        $content_intro = str_replace('\r','',$content_intro);
        $content_intro = str_replace('\n','',$content_intro);

        return $content_intro;
    }

    // 点赞
    public function getZanedAttribute()
    {
        if(!$user_id = auth() -> id()){
            return false;
        }
        $where = [
            'village_things_id' => $this -> id,
            'operate_type'   => 1,
            'user_id'        => $user_id,
            'obj_id'         => 0,
        ];

        return VillageThingsOperateLogs::where($where) -> first()
            ? true : false;

    }

    public function getFollowedAttribute()
    {
        if(!$user_id = auth() -> id()){
            return false;
        }

        return UserFollow::where('user_id',$user_id) -> where('follow_user_id',$this -> user_id) -> first()
            ? true : false;
    }

    /*
     * 存储预约内容
     * @return array
     */
    public static function store($param){

        if($param['imgs']){
            $arr = [];
            foreach(explode(',',$param['imgs']) as $v){
                $arr[] = $v;
            }
            $param['imgs'] = json_encode($arr);
            $param['types'] = 1;
        }

        if($param['video']){
            $param['types'] = 2;
        }

        $param['user_id'] = auth() -> id();
        if(User::where('id',$param['user_id']) -> value('role') == 1){
            $param['is_village_admin_things'] = 1;
        }else{
            $param['is_village_admin_things'] = 0;
        }

        $res = self::create($param);
        return $res;
    }

}
